import axios from 'axios'
import store from '@/store'
import JSONBig from 'json-bigint'
// console.log(store)
// 这里通过axios.create来创建多个不同的axios的实例，以备不时之需
const instance = axios.create({
  baseURL: 'http://ttapi.research.itcast.cn',
  transformResponse: [function (data) {
    try {
      return JSONBig.parse(data)
    } catch (err) {
      return data
    }
  }]
})

instance.interceptors.request.use((config) => {
  // 检查一下vuex中是否有token,如果有的话就拿出来设置到请求头里面
  const token = store.state.token.token
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
}, (error) => {
  return Promise.reject(error)
})

export default instance
